Essential Python Web Tools: Installation and Dependency Management of Virtual Environment venv
Why are virtual environments needed? They resolve dependency conflicts between different projects (e.g., compatibility issues between Django 2.2 and 4.0), prevent pollution of the system Python environment, and facilitate shared dependencies in team collaboration. Python 3.3+ includes the built-in `venv` module, which is a lightweight tool for creating virtual environments without requiring additional installations. Steps to use: 1. **Create**: Navigate to the project directory and run `python -m venv venv` to generate an independent `venv` folder. 2. **Activate**: Commands vary by system: Use the appropriate path for activation in Windows (cmd/PowerShell) or Mac/Linux. A successful activation will show `(venv)` in the terminal. 3. **Verify**: Run `python --version` and `pip --version` to confirm the environment is active. 4. **Dependency Management**: Install packages with `pip install` while activated. After installation, export dependencies with `pip freeze > requirements.txt`. For a new environment or another project, install dependencies quickly using `pip install -r requirements.txt`. 5. **Deactivate and Delete**: Exit with `deactivate`, and delete the `venv` folder directly to remove the environment. `venv` effectively isolates project dependencies, ensuring safety and efficiency, making it an essential tool for Python development.
Read More